package com.qianniu.napi.admin.service.akka;

import akka.actor.UntypedActor;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by chenxuebin on 2017/7/30.
 */
public class SignActor extends UntypedActor {

    private final static Logger logger = LoggerFactory.getLogger(SignActor.class);
//    private static final Log logLoseData = LogFactory.getLog("loseDat");

    @Override
    public void onReceive(Object message) {

        if (message instanceof String) {
            String msg = message.toString();
            logger.debug(">>>>>> SignActor receive message:" + msg);

            // 格式 type+uid+toke
            String[] arr = msg.split("@");
            if (arr.length < 3) return;
            String uid = arr[0];
            String latitude = arr[1];
            String longitude = arr[2];
            String precision = arr[3];
            String evtype = arr[4];
            String evtkey = "";
            if(arr.length>=6){
                evtkey = arr[5];
            }

            saveSign(uid,latitude,longitude,precision,evtype,evtkey);

            logger.debug(">>>>>> SignActor saveSign success");
        }
    }

    private void saveSign(String uid,String latitude,String longitude,
                          String precision,String evtype,String evtkey){
//        logger.debug(">>>>>> uid="+uid+" latitude="+latitude+" longitude="+longitude+" precision="+precision+" evtype="+evtype+" evtkey="+evtkey);
//
//        String isZaoqiKey = Constants.REDIS_KEYTYPE_IS_ZAOQIN + uid + "_" + DateUtil.getToday();
//        if (!Constants.checkMap.containsKey(isZaoqiKey))
//            Constants.checkMap.put(isZaoqiKey, new AtomicInteger(0));
//
//        Map map = new HashMap<String, Object>();
//
//        map.put("uid", uid);
//        map.put("signinTime", DateUtil.getToday());
//        map.put("stype", 1);
//
//        SigninMapper signinMapper = (SigninMapper) SpringUtil.getObject("signinMapper");
//        List<SigninDO> signins = signinMapper.selectSignin(map);
//        logger.debug(">>>>>> SignActor signins.size()="+signins.size());
//        map.remove("stype");
//
//        if(Strings.isNullOrEmpty(latitude)||latitude.equals("null")){
//            map.put("latitude", 0.0);
//            map.put("longitude", 0.0);
//            map.put("precision", 0.0);
//        }else{
//            map.put("latitude", Double.valueOf(latitude));
//            map.put("longitude", Double.valueOf(longitude));
//            map.put("precision", Double.valueOf(precision));
//        }
//
//        map.put("evtype", evtype);
//        map.put("evtkey", evtkey);
//
//        boolean isFirst = false;
//
////        && DateUtil.getHour()>=4&&DateUtil.getHour()<9
//        if ((evtype.equals(MessageUtil.EVENT_TYPE_LOCATION)
//                || evtype.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)
//                || evtype.equals(MessageUtil.EVENT_TYPE_CLICK)
//                || evtkey.equals(Constants.WX_EVT_SIGNIN))
//                && signins.size() == 0
//                && RedisUtils.isNullOrEmpty(isZaoqiKey)
//                && Constants.checkMap.get(isZaoqiKey).compareAndSet(0, 1)) {
//
//            logger.debug(">>>>>> SignActor set stype=1 for uid="+uid);
//            map.put("stype", 1);
//            isFirst = true;
//            RedisUtils.set(isZaoqiKey,Constants.REDIS_ONE, 1, TimeUnit.DAYS);
//
//        } else {
//
//            logger.debug(">>>>>> SignActor set stype=0 for uid="+uid);
//            map.put("stype", 0);
//        }
//
//        if (evtype.equals(MessageUtil.EVENT_TYPE_LOCATION)) {
//            if (signins.size() == 0) {
//                signinMapper.insertSignin(map);
//                logger.debug(">>>>>> set isZaoqiKey=" + isZaoqiKey);
//            }
//        } else {
//            signinMapper.insertSignin(map);
//        }
//
//        // 新用户 直接创建打开图片
//        if(isFirst) {
//            logger.debug(">>>>>> SignActor start to notify AKRealTemplate.signin()");
//            AkkaServer.system.actorOf(Props.create(AKRealTemplate.class))
//                    .tell(Constants.WX_EVT_SIGNIN + "@" + uid + "@qianbaidu@false", ActorRef.noSender());
//        }
//
//        logger.debug(">>>>>> saveSign over");
    }

}
